package com.lingdian.busapp.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lingdian.busapp.R;

public class BusListAdapter extends BaseAdapter{

	private List<JSONObject> mData;
	private JSONObject jsonObj ;
	private Context context;
	private int mRecource;
	
	public BusListAdapter(JSONObject jsonObj, Context context, int mRecource) {
		super();
		this.jsonObj = jsonObj;
		this.context = context;
		this.mRecource = mRecource;
		String busList = jsonObj.getString("busList");
		mData = JSONArray.parseArray(busList, JSONObject.class);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView==null){
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(mRecource, parent,false);
			holder.toPlace = (TextView) convertView.findViewById(R.id.item_bus_toPlace);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		JSONObject row = mData.get(position);
		if(row==null){
			return null;
		}
		holder.toPlace.setText(row.getString("toPlace"));
		return convertView;
	}
	static class ViewHolder{
		TextView toPlace;
	}

}
